/**
 * Created by Administrator on 2017/11/1.
 * Copyright 2017-2018 汉威.智慧环保事业部 Limited.
 * All rights reserved.
 */
package com.hw.one.collect.radar.Listener;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hw.one.collect.radar.service.RadarPicService;
import com.rabbitmq.client.Channel;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.core.ChannelAwareMessageListener;

import javax.annotation.Resource;

public class RadarPicReceiver implements ChannelAwareMessageListener {

    protected Logger log = LoggerFactory.getLogger(this.getClass());
    @Resource
    private RadarPicService radarPicService;

    @Override
    public void onMessage(Message message, Channel channel) throws Exception {
        try {
//            System.out.println("consumer--:" + message.getMessageProperties().getDeliveryTag() + ":" + new String(message.getBody()));
            // 雷达状态数据暂不做处理
//            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
            JSONObject json = (JSONObject) JSON.parse(new String(message.getBody()));
            log.info("监听到雷达历史图片：" + json.getString("startTime"));
            if (StringUtils.isBlank(json.getString("startTime")) || StringUtils.isBlank(json.getString("endTime"))
                    ||StringUtils.isBlank(json.getString("base64")) || StringUtils.isBlank(json.getString("siteCode"))
                    || StringUtils.isBlank(json.getString("SNCode"))) {
                channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
                return;
            }
            radarPicService.saveRadarPic(json);
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (Exception e) {
            e.printStackTrace();
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
//            channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, false);
        }
    }
}